package com.security.demo.service;

import com.security.demo.entity.MyUserDetail;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author xsy
 * @createTime 2023/2/5  13:16
 * @description
 * @since 1.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    //假设username = admin   password = 123  角色ROLE_ADMIN
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //实际情况，这里应该是要去数据库根据username查询用户，为了简单演示这里就写死数据
        //用户的角色也应该要去数据库查询，为了简便这里就写死数据
        String role = "ROLE_ADMIN";

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //数据库里的用户密码应该是加密后的，所以这里加密模拟一下
        String password = passwordEncoder.encode("123");
        //return new MyUserDetail(username,password,authorities);
        return new MyUserDetail(username,password,role);
    }

}
